#include <iostream>
#include <string>
#include <winsock.h>
#define PORT 4444
# define ansver "received\n"
#pragma comment(lib, "wsock32.lib")

void main ()
{
	int server_socket = 0, connect_socket = 0, bnd = 0, lstn = -1;
	// wsock32.dll initialisation
	WSADATA WsaData;
	int err = WSAStartup (0x0101, &WsaData);
	if (err == SOCKET_ERROR)
	{
           printf ("WSAStartup() failed: %ld\n", GetLastError ());
           return ;
	}
	server_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); /* socket opening
	AF_INET - for TCP/IP, SOCK_STREAM - for connecting, IPPROTO_TCP - TCP protocol*/
	SOCKADDR_IN s_in, s_from; // socket initialisation
	int client_buf_size  = sizeof(s_from);
	s_in.sin_family = AF_INET; // protocol type
	s_in.sin_port = PORT; // using port number
	s_in.sin_addr.s_addr = INADDR_ANY; // licalhost adress is unknown
	bnd = bind( server_socket, (LPSOCKADDR)&s_in, sizeof(s_in) ); /* binding  socket my_socket to port PORT
	my_socket - socket ID, (LPSOCKADDR)&s_in - adress of port initialising data
	(LPSOCKADDR)&s_in  - size (in bytes )  of s_in*/
	lstn = listen (server_socket, 5); // socket polling, can receive 5 connection requests
	char buffer [256];
	while (true)
	{
	connect_socket = accept (server_socket, (LPSOCKADDR)&s_from, &client_buf_size); /* receiving client request
	server_socket - socket for data receiving ,
	(LPSOCKADDR)&s_from - adress of client port initialising data, &client_bif_size - 
	accept functions returns new socket ID*/
		while(recv(connect_socket,buffer,sizeof(buffer),0)!=SOCKET_ERROR)// receiving client data
		{
         	printf("%c",buffer);
         	send(connect_socket,ansver,sizeof(ansver),MSG_DONTROUTE); 
		}


	}
}
